Imports System.Data
Partial Class Admin_frmPurchaseCart
    Inherits System.Web.UI.Page
    Dim _Bo As New Bo
    Dim _PurchaseDetailVo As New PurchaseDetailVo
    Dim _PurchaseId As String
    Dim _EmployeeId As String
    Dim _PurchaseMaserVo As New PurchaseMasterVo
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _EmployeeId = Session("EmpId")
        _PurchaseId = Request.QueryString("PurchaseId")
        If _PurchaseId = "" Then
            Response.Redirect("../Admin/frmTabPurchase.aspx")
        End If
        If _EmployeeId = "" Then
            Response.Redirect("../Client/frmLogin.aspx")
        End If
        Call GridData()
        Call GetSupplierData()
    End Sub
    Sub GetSupplierData()
        Dim dtSupplier As New DataTable
        dtSupplier = _Bo.getSupplierDetails
        ddlSupplier.DataSource = dtSupplier
        ddlSupplier.DataValueField = dtSupplier.Columns(0).ToString
        ddlSupplier.DataTextField = dtSupplier.Columns(1).ToString
        ddlSupplier.DataBind()
    End Sub
    Sub GridData()
        Dim dt As New DataTable
        dt.Clear()
        dt = _Bo.getPurchaseCart(_PurchaseId)
        If dt.Rows.Count = 0 Then
            MsgBox("NO PRODUCTS IN CART !")
            Response.Redirect("../Admin/frmTabPurchase.aspx")
        End If
        gdPurchaseCart.DataSource = dt
        gdPurchaseCart.DataBind()
    End Sub
    Protected Sub gdPurchaseCart_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gdPurchaseCart.DeleteCommand
        _Bo.DeleteFromPurchaseCart(_PurchaseId, e.Item.Cells(1).Text)
        GridData()
        Response.Redirect("frmShowCart.aspx")
    End Sub
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim dtPurchaseCart As New DataTable
        Dim dt1 As New DataTable
        Dim i As Integer
        Dim TotQty As Integer = 0
        dtPurchaseCart = _Bo.getPurchaseCart(_PurchaseId)
        For i = 0 To dtPurchaseCart.Rows.Count - 1
            _PurchaseDetailVo.PurchaseId = _PurchaseId
            _PurchaseDetailVo.ProductId = dtPurchaseCart.Rows(i).Item("ProductId").ToString
            _PurchaseDetailVo.quantity = dtPurchaseCart.Rows(i).Item("Quantity").ToString
            _PurchaseDetailVo.Price = dtPurchaseCart.Rows(i).Item("Price").ToString
            _Bo.insertPurchaseDetail(_PurchaseDetailVo)
            dt1 = _Bo.getQuantity(dtPurchaseCart.Rows(i).Item("ProductId").ToString)
            TotQty = Val(dt1.Rows(0).Item(0).ToString) + Val(dtPurchaseCart.Rows(i).Item("Quantity").ToString)
            _Bo.UpdateOnPurchase(dtPurchaseCart.Rows(i).Item("ProductId").ToString, TotQty)
            TotQty = 0
        Next
        Call Calculate()
    End Sub
    Sub Calculate()
        Dim dtTotal As New DataTable
        dtTotal = _Bo.getPurchaseTotal(_PurchaseId)
        _PurchaseMaserVo.PurchaseId = _PurchaseId
        _PurchaseMaserVo.PurchaseDate = ((Now.ToString).Substring(0, 10))
        _PurchaseMaserVo.TotalQuantity = dtTotal.Rows(0).Item(0).ToString
        _PurchaseMaserVo.TotalAmount = dtTotal.Rows(0).Item(1).ToString
        _PurchaseMaserVo.SupplierId = ddlSupplier.SelectedValue.ToString
        _Bo.AddToPurchaseMaster(_PurchaseMaserVo)
        MsgBox("Products Added")
        _Bo.DeletePurchaseCart()
        Session("EmpId") = _EmployeeId
        Response.Redirect("frmHome.aspx")
    End Sub
    Protected Sub gdPurchaseCart_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gdPurchaseCart.EditCommand
        Session("EmpId") = _EmployeeId
        Response.Redirect("EditPurchaseCart.aspx?ProductId=" & e.Item.Cells(1).Text)
    End Sub
    Protected Sub btnAddMore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMore.Click
        Session("EmpId") = _EmployeeId
        Response.Redirect("frmTabPurchase.aspx")
    End Sub
End Class
